﻿using System.Linq;

namespace Garfield.Core.Runtime
{
    public static class Adapters
    {
        internal static IExtensionPoint<IAdapterFactory> AdapterFactories;

        public static AdapterType CreateAdapter<AdapterType>(this object adaptee)
        {
            var adapterFactory = AdapterFactories.Extensions.FirstOrDefault(factory => factory.CanBeAdaptedTo(adaptee, typeof(AdapterType)));
            return (AdapterType) (adapterFactory != null ? adapterFactory.CreateAdapter(adaptee, typeof(AdapterType)) : default(AdapterType));
        }
    }
}
